package com.bafa.util.comparators;


import java.util.Comparator;

import com.bafa.objects.team.Team;
import com.bafa.objects.team.TeamSeasonStats;
import com.bafa.services.RetrieveServices;

public class LeagueComparator implements Comparator {
private int seasonId;
	public int compare(Object team1, Object team2) {
		Team teamObj1 = RetrieveServices.getTeam(team1.toString());
		//System.out.println(teamObj1.getId());
		Team teamObj2 = RetrieveServices.getTeam(team2.toString());
		//System.out.println(teamObj2.getStats());
		TeamSeasonStats seasonStats1 = (TeamSeasonStats)teamObj1.getStats().getSeasonStats().get(seasonId);
                TeamSeasonStats seasonStats2 = (TeamSeasonStats)teamObj2.getStats().getSeasonStats().get(seasonId);
                
                int points1 = (seasonStats1.getLeagueGamesWon()*3)+seasonStats1.getLeagueGamesDrawn();
                int points2 = (seasonStats2.getLeagueGamesWon()*3)+seasonStats2.getLeagueGamesDrawn();
                
                if(points1!=points2)
                    return points1-points2;
                else{
                    if(seasonStats1.getLeagueGoalsScored()!=seasonStats2.getLeagueGoalsScored())
                        return seasonStats1.getLeagueGoalsScored()-seasonStats2.getLeagueGoalsScored();
                    else{
                        if((seasonStats1.getLeagueGoalsScored()-seasonStats1.getLeagueGoalsConceded())!=(seasonStats2.getLeagueGoalsScored()-seasonStats2.getLeagueGoalsConceded()))
                            return (seasonStats1.getLeagueGoalsScored()-seasonStats1.getLeagueGoalsConceded())-(seasonStats2.getLeagueGoalsScored()-seasonStats2.getLeagueGoalsConceded());
                        else{
                            return 0;
                        }
                    }
                }
	}
        
        public LeagueComparator setSeasonId(int seasonId){
            this.seasonId = seasonId;
            return this;
        }

}
